home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / sfxserve.000 / sfxserve / sfxserver-0.02 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-10  |  5.2 KB  |  218 lines

  1. /*
  2.  * ---------------------------------------------------------------------------
  3.  * sfxserver/main.c
  4.  *
  5.  * Copyright by Terry Evans 1994
  6.  * tevans@cs.utah.edu, tevans@slc.unisys.com
  7.  * ---------------------------------------------------------------------------
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are
  11.  * met: 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer. 2.
  13.  * Redistributions in binary form must reproduce the above copyright notice,
  14.  * this list of conditions and the following disclaimer in the documentation
  15.  * and/or other materials provided with the distribution.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  * ---------------------------------------------------------------------------
  29.  */
  30.  
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37.  
  38.  
  39. #include "global.h"
  40. #include "types.h"
  41. #include "error.h"
  42. #include "device.h"
  43. #include "sample.h"
  44. #include "channel.h"
  45. #include "mix.h"
  46. #include "io.h"
  47. #include "main.h"
  48.  
  49.  
  50. /* File descriptor for the sound device */
  51. static int s_fd = -1;
  52.  
  53. /* Fragment size for the device (returned from ioctl call) */
  54. static int frag_size = 0;
  55.  
  56. /* Fragment specification for sound device */
  57. static int frag_spec = FRAG_SPEC;
  58.  
  59. /* Playback frequency rate */
  60. static int frequency = SAMPLE_RATE;
  61.  
  62. /* Support stereo sound */
  63. static int stereo = TRUE;
  64.  
  65. /* Input buffer */
  66. char command;
  67.  
  68. /* Filename to load */
  69. char filename[MAX_BUFFSIZE];
  70.  
  71. /* Temporary buffer */
  72. char buffer[MAX_BUFFSIZE];
  73.  
  74. /* For now we have 8 channels (in global.h) */
  75. static channel_t *channel[MAX_CHANNELS];
  76. static sample_t *sample[MAX_SAMPLES];
  77. static mix_t mix;
  78.  
  79.  
  80. void main(int argc, char *argv[])
  81. {
  82.   fd_set fdvar;
  83.   struct timeval t_val = { 0L, 1L};
  84.   int sample_num;
  85.   int channel_num;
  86.   int volume_level;
  87.   int left;
  88.   int right;
  89.   char side;
  90.   unsigned int volume;
  91.  
  92.   /* Initialize the sound device */
  93.   s_fd = D_Init("/dev/dsp", &frag_spec, &frequency, &stereo, &frag_size);
  94.  
  95.   S_Init(sample);
  96.   C_Init(channel);
  97.   M_Init(&mix, frag_size);
  98.  
  99.   /* Send out the ready message */
  100.   IO_WriteStdout(READY);
  101.  
  102.   /* Clear out the file handles */
  103.   FD_ZERO(&fdvar);
  104.  
  105.   /* Now, just go on forever */
  106.   for(;;)
  107.   {
  108.     /* Set the stdio handle */
  109.     FD_SET(0, &fdvar);
  110.  
  111.     /* Execute the select call to see if anything is ready to read in */
  112.     if(select(1, &fdvar, NULL, NULL, &t_val) < 0)
  113.      E_ErrnoNonFatalError("main", "select() call failed");
  114.  
  115.     if(FD_ISSET(0, &fdvar))
  116.     {
  117.       if(read(0, &command, 1) < 0)
  118.       {
  119.     E_ErrnoFatalError("main", "read failed on stdin");
  120.     continue;
  121.       }
  122.  
  123.       /* Flush all entries */
  124.       if(command == 'c')
  125.       {
  126.     /* Read in the new line */
  127.     read(0, &command, 1);
  128.  
  129.     C_DeInit(channel);
  130.     S_DeInit(sample);
  131.     continue;
  132.       }
  133.  
  134.       /* Load in a new data file */
  135.       if(command == 'l')
  136.       {
  137.     scanf("%s", filename);
  138.  
  139.     /* Replace the newline with a null */
  140.     filename[strlen(filename)] = '\0';
  141.  
  142.     if((sample_num = S_LoadRawSample(filename, sample)) >= 0)
  143.         {
  144.       sprintf(buffer, "s%2.2x\n", sample_num);
  145.       IO_WriteStdout(buffer);
  146.     }
  147.  
  148.     continue;
  149.       }
  150.  
  151.       if(command == 'p')
  152.       {
  153.     scanf("%2x%2x%2x", &sample_num, &left, &right);
  154.  
  155.     if(S_VerifySample(sample_num))
  156.       if((channel_num = C_AllocChannel(channel, sample[sample_num],
  157.                        left, right)) >= 0)
  158.           {
  159.         sprintf(buffer, "c%2.2x\n", channel_num);
  160.         IO_WriteStdout(buffer);
  161.       }
  162.     else
  163.       E_NonFatalError("main", "sample %ul is not loaded", sample_num);
  164.  
  165.     continue;
  166.       }
  167.  
  168.       if(command == 'v')
  169.       {
  170.     if(read(0, &side, 1) < 0)
  171.         {
  172.       E_FatalError("main", "read() failed from stdin");
  173.       continue;
  174.     }
  175.  
  176.     scanf("%2x%2x", &channel_num, &volume);
  177.  
  178.     sprintf(buffer, "side    = %c\n", side);
  179.     IO_WriteStdout(buffer);
  180.  
  181.     sprintf(buffer, "channel = %d\n", channel_num);
  182.     IO_WriteStdout(buffer);
  183.  
  184.     sprintf(buffer, "volume  = %d\n", volume);
  185.     IO_WriteStdout(buffer);
  186.  
  187.     if((volume_level = C_ChangeVolume(channel, side, channel_num, volume)) >= 0)
  188.         {
  189.       sprintf(buffer, "v%2.2x\n", volume_level);
  190.       IO_WriteStdout(buffer);
  191.     }
  192.     
  193.     continue;
  194.       }
  195.  
  196.       /* Quit the program */
  197.       if(command == 'q')
  198.       {
  199.     /* Read in the new line */
  200.     read(0, &command, 1);
  201.     break;
  202.       }
  203.     }
  204.  
  205.     M_MixChannels(&mix, channel, frag_size, s_fd, stereo);
  206.   }
  207.  
  208.   M_DeInit(&mix);
  209.   C_DeInit(channel);
  210.   S_DeInit(sample);
  211.   D_DeInit(s_fd);
  212.  
  213.   /* Inform the server to exit */
  214.   IO_WriteStdout("q");
  215.  
  216.   exit(0);
  217. }
  218.